<?php
/**
 * 1.活动列表
 */

namespace app\wap\controller;

use app\index\business\Wx;
use app\libs\Functions;
use think\Request;
use think\View;
use think\Controller;

class Index extends Controller
{
    protected $url='';
    /**
     * 1、大厅活动列表
     * @return \think\response\Json
     */
    public function index()
    {
        if (isset($_GET['username'])) {
            $data = $_GET;
            /****验证账号和密码****/
            $user = db('user')->where(['status' => 1, 'phone' => $data['username']])->find();
            if (empty($user)) {
                echo json_encode(['code' => 0, 'msg' => "用户不存在"]);
                die;
            }
            if ($user['phone'] != $data['username']) {
                echo json_encode(['code' => 0, 'msg' => "账号或密码不正确"]);
                die;
            }
            $password = Functions::encryptPassword($data['password'], $user['password_salt']);
            if ($password != $user['password']) {
                echo json_encode(['code' => 0, 'msg' => "账号或密码不正确"]);
                die;
            }
            /****判断用户是否关注****/
            $wx = new Wx();
            $access_token = $wx->get_access_token();
            $info = $wx->get_userinfo($access_token, $data['openid']);
            if ($info['subscribe'] ==0){
                echo json_encode(['code' => 0, 'msg' => '没有关注啊']);
                die;
            }
            /****update****/
            $info['openid'] = $data['openid'];
            if (db('user')->where('user_no', $user['user_no'])->update(['openid' => $data['openid']])) {
                echo json_encode(['code' => 1, 'msg' => "绑定成功"]);
                die;
            } else {
                echo json_encode(['code' => 0, 'msg' => "绑定失败"]);
                die;
            }


            echo json_encode($data);
            die;
        } else {

            //从数据查询

            $wx = db('wx_info')->find();
            $appid = trim($wx['appid']);
            $secret = trim($wx['sercet']);
            if (!empty($_GET['code'])) {
                $code = $_GET['code'];
            }


            if (empty($code)) {
                $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


                $forward = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . urlencode($url) . "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
                header('Location: ' . $forward);
                exit;
            }
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
            $response = file_get_contents($url);

            $response = json_decode($response, true);
            // echo '<pre>';
            // print_r($response);

            $view = new View();
            $view->assign('openid', $response['openid']);
            //$view->assign('openid',1111);
            return $view->fetch();

        }

    }

    function getOauthUserInfoUrl1($callback, $state = '', $appid)
    {
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$callback}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
    }
}
